package com.show.mytest;

public class Test04_Synchronized {
    private static int i = 100;
    public static void main(String[] args) {
       // 创建一个Runnable对象
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized ("lock") {
                        if (i < 1) {
                            break;
                        }
                        System.out.println(Thread.currentThread().getName() + " 售出了第 " + i-- + " 张票");
                    }
                }
            }
        };
        // 创建两个线程
        Thread thread1 = new Thread(runnable,"小丽");
        Thread thread2 = new Thread(runnable,"小美");
        thread1.start();
        thread2.start();
    }
}
